// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require(CONFIG.BasePath + "js/enterprise/base");
	var router = require("../hash/pageRouter");
	var top = require("../top/top_main");
	var footer = require("../footer/footer_main");
	var constant = require("constant");
	var auditState;

	var init = function() {
		top.init(0);
		footer.init();
		router.init();
//		inquireCurrentEnterpriseDetail();
		$("#mainPage").addClass("nav_active");
		$("#head").removeClass("none");
		$("#content").removeClass("none");
		$("#footer").removeClass("none");
		inquireBulkstockGatherLatestList();
		inquireBulletinList();
		inquireFinanceProductPageList();
		inquireStatisticsIndex();
		inquireStatisticsEndallPageList();
		inquireAdListByReferenceNo();
		initEvent();
	};
	
	var initEvent = function() {

		$("#content").on("click", ".bulletinIdDetail", function() {
			window.open("../main/bullitinDetail.html?bulletinId=" + $(this).attr("bulletinId"));
		});
		
		$(".monitor").unbind("click").click(function(){
			location.href = "../others/monitor.html";
		});
		
		if (base.getData("sid") != null && base.getData("sid") != "") {
			$(".login_title5").addClass("visibility");
		}
	};

	//获得用户信息
	var inquireCurrentEnterpriseDetail = function() {
		base.fnAjax({
			action: "inquireCurrentEnterpriseDetail.json",
			async: false,
			oData: {}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					auditState = data.datas[0].auditState;
				} else {}
			}
		});
	};

	//查询产品列表
	var inquireFinanceProductPageList = function() {
		base.fnAjax({
			action: "inquireFinanceProductPageList.json",
			nocode: true,
			oData: {
				pageIndex: 1,
				pageSize: 2,
				state: 4,
				productScope: 1
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#mainFinanceProductListTpl").html();
					var html = juicer(tpl, data);
					$(".huorongbao_item").html(html);
					$(".instantlyFinance").unbind("click").click(function() {
						inquireCurrentEnterpriseDetail();
						var productId = $(this).attr("id");
						if(auditState == 2){
							location.href = '../apply/apply_main.html?productType=' + productType;
						} else{
//							base.alert(2, "您的账户还未进行资质认证，暂不能发起融资!");
							location.href = '../approve/approve_main.html';
						}
					});

					$(".productDetail").unbind("click").click(function() {
						var productId = $(this).attr("id");
						location.href = '../product/productDetail.html?productId=' + productId;
					});

				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}
		//大宗商品行情表
	var inquireBulkstockGatherLatestList = function() {
		base.fnAjax({
			action: "inquireBulkstockGatherLatestList.json",
			nocode: true,
			oData: {
				pageSize: 6
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#mainBulkstockListTpl").html();
					var html = juicer(tpl, data);
					//              	document.getElementById("mainBulkstockListBody").innerHTML = html;
					$("#mainBulkstockListBody").html(html);
					//					添加监测跳转
					$(".publicMonitor").unbind("click").click(function() {
						var bulkstockname = $(this).parent().parent().attr("bulkstockname");
						var specification = $(this).parent().parent().attr("specification");
						location.href = "../publicInquire/publicInquireMonitor.html?bulkstockName=" + encodeURI(encodeURI(bulkstockname)) + "&specification=" + encodeURI(encodeURI(specification));
					});
					riseAndFallColor();
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	//查询广告信息  
	var inquireAdListByReferenceNo = function() {
		base.fnAjax({
			action: "inquireAdListByReferenceNo.json",
			nocode: true,
			oData: {
				referenceNo: "Enterprise00001"
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var lunbos = [];
					$.each(data.datas, function() {
						var lunbo = {
							fileUuid: "",
							seqNo: "",
							url: ""
						};
						lunbo.fileUuid = this.fileUuid;
						lunbo.seqNo = this.seqNo;
						lunbo.url = this.url;
						lunbos.push(lunbo);
					});

					lunbos.sort(by("seqNo"));

					var i = 0;
					$("#content .w1366").attr("style", "background-image:url(/finance/api/v1.0/downloadFile.file?fileUuid=" + lunbos[0].fileUuid + ")");
					setInterval(function() {
						if (i < lunbos.length) {
							$("#content .w1366").attr("style", "background-image:url(/finance/api/v1.0/downloadFile.file?fileUuid=" + lunbos[i].fileUuid + ")");
//							$("#content .w1366").unbind("click").click(function() {
//								location.href = lunbos[i].url;
//								//								location.href = "http://www.baidu.com";
//							});
						} else {
							i = 0;
							$("#content .w1366").attr("style", "background-image:url(/finance/api/v1.0/downloadFile.file?fileUuid=" + lunbos[i].fileUuid + ")");
//							$("#content .w1366").unbind("click").click(function() {
//								location.href = lunbos[i].url;
//							});
						}
						i++;
					}, 5000);
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	function by(propertyName) {
		return function(object1, object2) {
			var value1 = object1[propertyName];
			var value2 = object2[propertyName];
			if (value2 <= value1) {
				return -1;
			} else if (value2 > value1) {
				return 1;
			} else {
				return 0;
			}
		}
	};

	//改变涨跌颜色
	var riseAndFallColor = function() {
		var riseAndFall = $(".riseAndFall");
		for (var i = 0, j = riseAndFall.length; i < j; i++) {
			if (riseAndFall[i].innerHTML.charAt(0) == "-") {
				$(riseAndFall[i]).css("color", "#33A55E");
			} else if (riseAndFall[i].innerHTML.charAt(0) == "+") {
				$(riseAndFall[i]).css("color", "#F1592A");
			} else {
			}
		}
	};

	//查询公告列表
	var inquireBulletinList = function() {
		base.fnAjax({
			action: "inquireEnterpriseBulletinList.json",
			nocode: true,
			oData: {
				pageSize: 9
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#bulletinListTpl").html();
					var html = juicer(tpl, data);
					$("#bulletinListBody").html(html);
				} else {}
			}
		});
	};
	//查询首页统计信息
	var inquireStatisticsIndex = function() {
			base.fnAjax({
				action: "inquireStatisticsIndex.json",
				nocode: true,
				oData: {}, //传递参数
				fnSuccess: function(data) {
					if (data.status == 200) {
						$(".finishedAmount").html(data.datas[0].finishedAmount);
						$(".endallCount").html(data.datas[0].endallCount);
						$(".registered").html(data.datas[0].registered);
						$(".enterpriseCount").html(data.datas[0].enterpriseCount);
						$(".totalFinanceAmount").html("&#65509;" + data.datas[0].totalFinanceAmount);
						$(".totalFinanceCount").html(data.datas[0].totalFinanceCount);

						$(".month").html(getPreMonth() + "月份");
					} else {
						base.alert(3, data.message);
					}
				}
			});
		}
		//查询首页统计信息-成功借款记录
	var inquireStatisticsEndallPageList = function() {
			base.fnAjax({
				action: "inquireStatisticsEndallPageList.json",
				nocode: true,
				oData: {
					pageSize: 3
				}, //传递参数
				fnSuccess: function(data) {
					if (data.status == 200) {
						var tpl = $("#statisticsEndallTpl").html();
						var html = juicer(tpl, data);
						$("#statisticsEndallBody").html(html);
					} else {
						base.alert(3, data.message);
					}
				}
			});
		}
		//获取上一个月份
	var getPreMonth = function() {
		var myDate = new Date();
		var month = myDate.getMonth(); //获取当前日期的月份
		month = month + 1;
		var month2 = parseInt(month) - 1;
		if (month2 == 0) {
			month2 = 12;
		}
		return month2;
	}
	return {
		init: init
	};
});